package com.easylinkin.linkappapi.devicemanager.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.devicemanager.entity.SensorEntity;
import com.easylinkin.linkappapi.devicemanager.entity.WireropeEntity;
import com.easylinkin.linkappapi.devicemanager.mapper.WireropeMapper;
import com.easylinkin.linkappapi.devicemanager.service.SensorService;
import com.easylinkin.linkappapi.devicemanager.vo.WireropeVo;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author linWenLe
 * @Date: 2024/3/28
 * @Description: 设备传感器
 */
@Slf4j
@Service("sensorService")
public class SensorServiceImpl extends ServiceImpl<WireropeMapper, WireropeEntity> implements SensorService {

    private static final String[] DEVICE_TYPE_IDS = {"25c241d9ecb511ee9f580242ac110009", "280e333aecb511ee9f580242ac110009", "2d5219c5ecb511ee9f580242ac110009", "302902c4ecb511ee9f580242ac110009"};

    @Resource
    private WireropeMapper wireropeMapper;

    @Resource
    LinkappUserContextProducer linkappUserContextProducer;

    @Override
    public List<SensorEntity> sensorMonitoring() {
        List<String> list = Arrays.asList(DEVICE_TYPE_IDS);
        String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
        return wireropeMapper.sensorMonitoring(tenantId, list);
    }

    @Override
    public IPage<WireropeEntity> sensorAlertHistory(RequestModel<WireropeVo> restPage) {
        Page page = restPage.getPage();
        WireropeVo params = restPage.getCustomQueryParams();
        String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
        return wireropeMapper.sensorAlertHistory(page, params, Arrays.asList(DEVICE_TYPE_IDS), tenantId);
    }

    @Override
    public Map<String,Object> sensorDataList(WireropeVo entity) {
        Assert.notNull(entity.getDeviceId(),"设备id不能为空");
        List<Date> timeList = new ArrayList<>();
        List<String> resultList = new ArrayList<>();

        List<WireropeEntity> list = wireropeMapper.sensorDataList(entity);
        Date maxDateTime = new Date();
        BigDecimal result = new BigDecimal(0);
        for (WireropeEntity wirerope : list) {
            BigDecimal parsed = new BigDecimal(wirerope.getResult());
            if (parsed.compareTo(result) > 0) {
                result = parsed;
                maxDateTime = wirerope.getTime();
            }
            timeList.add(wirerope.getTime());
            resultList.add(wirerope.getResult());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("x",timeList);
        map.put("y",resultList);
        map.put("maxTime",new Object[]{maxDateTime, result});
        return map;
    }

}
